package com.base.third.yim

import com.base.config.BaseConfig
import com.base.utils.SHA1Utils
import com.dtflys.forest.annotation.*
import com.dtflys.forest.http.ForestRequest
import com.dtflys.forest.lifecycles.MethodAnnotationLifeCycle
import com.dtflys.forest.reflection.ForestMethod
import java.util.*

@MustBeDocumented
@RequestAttributes
@MethodLifeCycle(YIMAuthLifeCycle::class)
annotation class YIMAuth
class YIMAuthLifeCycle : MethodAnnotationLifeCycle<YIMAuth?, Any?> {
    override fun onMethodInitialized(method: ForestMethod<*>?, annotation: YIMAuth?) {}
    override fun beforeExecute(request: ForestRequest<*>): Boolean {
        val CurTime = Date().time / 1000
        val Nonce = "${CurTime}_${Random().nextInt(2147483647)}"
        request.addHeader(mapOf(
                "AppKey" to BaseConfig.yim_AppKey,
                "Nonce" to Nonce,
                "CurTime" to CurTime,
                "CheckSum" to SHA1Utils.encode("${BaseConfig.yim_AppSecret}$Nonce$CurTime"),
        ))
        return true
    }
}

@YIMAuth
@BaseRequest(
        baseURL = "${'$'}{yim.server}/user",
)
interface YIMClient_User {
    @Post("create.action")
    fun registerToken(
            @Body("accid") accid: String,
    ): Map<String, Any?>?
}